package cc.wenshixin.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cc.wenshixin.entity.AdminUser;

public class AdminUserDaoImpl extends BaseDaoImpl<AdminUser> implements AdminUserDao {
	@SuppressWarnings("all")
	@Override
	public AdminUser login(AdminUser adminUser) {
		HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
		List<AdminUser> adminUserList = (List<AdminUser>) hibernateTemplate.find("FROM AdminUser WHERE name=? AND password=?", adminUser.getName(), adminUser.getPassword());
		if(adminUserList != null && adminUserList.size() != 0) {
			AdminUser user = adminUserList.get(0);
			return user;
		}
		return null;
	}

	@SuppressWarnings("all")
	@Override
	public AdminUser findByName(String name) {
		HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
		DetachedCriteria criteria = DetachedCriteria.forClass(AdminUser.class);
		criteria.add(Restrictions.eq("name", name));
		List<AdminUser> adminUserList = (List<AdminUser>) hibernateTemplate.findByCriteria(criteria);
		if(adminUserList != null && adminUserList.size() > 0) {
			AdminUser adminUser = adminUserList.get(0);
			return adminUser;
		}
		return null;
	}

}
